package JianZhiOffer;

import JianZhiOffer.entity.ListNode;

/**
 * 链表反转
 * */
public class Solution24 {

    /**
     * 三指针
     * */
    public ListNode reverseList(ListNode head) {
        if(head == null) return head;
        ListNode prev = null;
        ListNode cur = head;
        ListNode next = null;
        while(cur != null){
            next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }

        return prev;

    }

    /**
     * 递归
     * 1>2>3>4>5
     * 假设2后面已经处理好，那么可以简化为1和f(2)的问题
     * 1和f(2),需要把2指向1，1指向null
     * =f(2) + head.next.next = head + head.next = null
     * */
}
